<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>支付页</title>
  <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>
<body>
<script >
  console.log(wx);
  function getUrlParam (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) { return unescape(r[2]) }
      return null
  }
  function configOrder () {
    $.ajax({
      url: host + '/magazine/api/order/' + window.sessionStorage.getItem('orderId') + '/pay',
      type: 'POST', // GET
      async: true,    // 或false,是否异步
      headers: {
        'TOKEN': window.sessionStorage.getItem('token')
      },
      timeout: 5000,    // 超时时间
      dataType: 'json',    // 返回的数据格式：json/xml/html/script/jsonp/text
      beforeSend: function (xhr) {
      },
      success: function (data, textStatus, jqXHR) {
        console.log(data)
        var pay = data.params.pay
        // alert(pay)
        payTest(pay, function () {
          // alert("支付成功");
          var url = host
          url = host + '/index.html#/orderSuccessfully?order=' + window.sessionStorage.getItem('orderId')
          window.location.replace(url)
        })
      },
      error: function (xhr, textStatus) {
        console.log('支付失败')
        var url = host
        url = host + '/index.html#/index'
        window.location.replace(url)
      },
      complete: function () {
        //console.log('结束');
      }
    })
  }
  function payTest (cof, callback) {
    function onBridgeReady () {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', cof,
        function (res) {
          console.log(res)
          if (res.err_msg == 'get_brand_wcpay_request:ok') { callback && callback() }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
            else {
              console.log('支付失败,请重新请求支付')
              window.history.go(-1)
            }
          }
          )
    }
    if (typeof WeixinJSBridge == 'undefined') {
      if (document.addEventListener) {
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
      } else if (document.attachEvent) {
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
      }
    } else {
      onBridgeReady()
    }
  }
  if (window.sessionStorage.getItem('orderId')) {
    var host = 'https://hljhouse.jizui.top'
    console.log(window.sessionStorage.getItem('orderId'))
    configOrder()
  }
</script>
</body>
</html>
